package cn.nexd.location.collector.core;

import android.content.Context;
import android.content.pm.PackageManager;

import java.util.List;

import cn.nexd.location.collector.NexdCollectorConfig;
import cn.nexd.location.collector.bean.BluetoothCollectorResult;
import cn.nexd.location.collector.bean.WifiCollectorResult;

/**
 * Created by codingfish on 15/9/28.
 */
public abstract class Collector implements ICollector {

    protected Context context;
    protected NexdCollectorConfig collectorConfig;

    private static int taskTimeOut;

    private static long task_id = System.currentTimeMillis();
    private static long currentTime = 0L;

    protected Collector(Context context, NexdCollectorConfig collectorConfig) {
        this.context = context;
        this.collectorConfig = collectorConfig;
        this.taskTimeOut = collectorConfig.wifiCollectorRate;
    }

    protected abstract boolean checkPermission();

    protected boolean checkPermission(String permission) {
        int res = context.checkCallingOrSelfPermission(permission);
        return (res == PackageManager.PERMISSION_GRANTED);
    }

    protected long getTaskId() {

        if (task_id == 0L) {
            task_id = System.currentTimeMillis();
        }
        currentTime = System.currentTimeMillis();
        if (currentTime + taskTimeOut <= System.currentTimeMillis()) {
            return task_id;
        } else {
            task_id = System.currentTimeMillis();
            return task_id;
        }
    }


    public interface BluetoothCollectorListener {
        /**
         * 采集失败.采集器可用但是因为某些暂时性的原因造成的无法采集,将会回调该方法.
         * <p></p>
         *
         * @param collectorResultTaskId
         * @param collectorResultTaskTime
         * @param collectorResultTaskFailedType
         * @param collectorResultTaskFailedMessage
         */
        void onFailed(long collectorResultTaskId, long collectorResultTaskTime, int collectorResultTaskFailedType, String collectorResultTaskFailedMessage);

        /**
         * 采集停止.由于某些原因,被视为采集器无法正常工作,则会回调该方法.
         *
         * @param collectorResultTaskId
         * @param collectorResultTaskTime
         * @param collectorResultTaskStopType
         * @param collectorResultTaskStopMessage
         */
        void onStop(long collectorResultTaskId, long collectorResultTaskTime, int collectorResultTaskStopType, String collectorResultTaskStopMessage);

        /**
         * 采集成功
         *
         * @param collectorResultTaskId
         * @param collectorResultTaskTime
         * @param bluetoothCollectorResults
         */
        void onSuccess(long collectorResultTaskId, long collectorResultTaskTime, List<BluetoothCollectorResult> bluetoothCollectorResults);

    }

    public interface WifiCollectorListener {

        /**
         * 采集失败.采集器可用但是因为某些暂时性的原因造成的无法采集,将会回调该方法.
         * <p></p>
         *
         * @param collectorResultTaskId
         * @param collectorResultTaskTime
         * @param collectorResultTaskFailedType
         * @param collectorResultTaskFailedMessage
         */
        void onFailed(long collectorResultTaskId, long collectorResultTaskTime, int collectorResultTaskFailedType, String collectorResultTaskFailedMessage);

        /**
         * 采集成功
         *
         * @param collectorResultTaskId
         * @param collectorResultTaskTime
         * @param wifiCollectorResults
         */
        void onSuccess(long collectorResultTaskId, long collectorResultTaskTime, List<WifiCollectorResult> wifiCollectorResults);

        /**
         * 采集停止.由于某些原因,被视为采集器无法正常工作,则会回调该方法.
         *
         * @param collectorResultTaskId
         * @param collectorResultTaskTime
         * @param collectorResultTaskStopType
         * @param collectorResultTaskStopMessage
         */
        void onStop(long collectorResultTaskId, long collectorResultTaskTime, int collectorResultTaskStopType, String collectorResultTaskStopMessage);
    }
}
